//
//  ZZCRegisterFourController.m
//  ZZC-Finance
//
//  Created by mac on 2017/5/21.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCRegisterFourController.h"
#import "ZZCLoginController.h"
#import "ZZCMeController.h"

@interface ZZCRegisterFourController ()

@property (strong, nonatomic) ZZCMenuView *menuView;
@property (assign, nonatomic) NSInteger is_company;
@end

@implementation ZZCRegisterFourController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"正资产账户注册";
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
//账号选择
- (IBAction)accountClick {
    ZZCLog(@"选择账号类型");
    NSArray *arr = @[@"个人",@"企业"];
    [self.menuView removeFromSuperview];
    self.menuView= [[ZZCMenuView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.accountView.frame), ZZCScreenW, 132) contentArray:arr];
    __weak typeof (ZZCMenuView *) weakSelf = self.menuView;
    __weak typeof (self) weakself = self;
    self.menuView.block = ^(BOOL isSeletct ,NSString *title){
        
        [weakSelf removeFromSuperview];
        if (title.length != 0) {
            [weakself.accountBtn setTitle:title forState:UIControlStateNormal];
            if ([title isEqualToString:@"个人"]) {
                weakself.is_company = 1;
            }else {
                weakself.is_company = 2;
            }
        }
    };
    
    [self.view addSubview:self.menuView];
}
//确定
- (IBAction)confirmClick {
    ZZCLog(@"确认");
    [self submitRegistration];

}

- (void)submitRegistration {
    if (self.openId == nil) {
        [self registration];
    }else {
        [self wxRegistration];
    }
}

- (void)registration {
    NSDictionary *dict = @{@"phone":self.phone,
                           @"password":self.password,
                           @"nickname":self.nameTextField.text,
                           @"is_company":@(self.is_company)};
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:sign];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,
@"phone":self.phone,@"password":self.password,@"nickname":self.nameTextField.text,@"is_company":@(self.is_company)};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_REGIST parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
            ZZCLog(@"-%@",responseObject[@"data"][@"token"]);
            
            if ([responseObject[@"code"]isEqual:@(200)]) {
                if ([[NSUserDefaults standardUserDefaults] objectForKey:@"token"] == nil) {
                    [[NSUserDefaults standardUserDefaults] setObject:responseObject[@"data"][@"token"] forKey:@"token"];
                }
                //跳回登录界面
                for (UIViewController *controller in self.navigationController.viewControllers) {
                    if ([controller isKindOfClass:[ZZCLoginController class]]) {
                        [self.navigationController popToViewController:controller animated:YES];
                        //这里可以做回到登录界面的一些数据操作
                    }
                }
            }
        }
    }];

}

- (void)wxRegistration {
    if (self.password.length < 6) {
        return;
    }
    NSDictionary *dict = @{@"phone":self.phone,
                           @"password":self.password,
                           @"nickname":self.nameTextField.text,
                           @"is_company":@(self.is_company),
                           @"open_id":self.openId,
                           @"head_img":self.iconUrl};
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],
                            @"sign":sign,
                            @"phone":self.phone,
                            @"password":self.password,
                            @"nickname":self.nameTextField.text,
                            @"is_company":@(self.is_company),
                            @"open_id":self.openId,
                            @"head_img":self.iconUrl};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_REGIST parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            ZZCLog(@"-%@",responseObject);
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
            if ([responseObject[@"code"]isEqual:@(200)]) {
                if ([[NSUserDefaults standardUserDefaults] objectForKey:@"token"] == nil) {
                    [[NSUserDefaults standardUserDefaults] setObject:responseObject[@"data"][@"token"] forKey:@"token"];
                }
                //跳回登录界面
                for (UIViewController *controller in self.navigationController.viewControllers) {
                    if ([controller isKindOfClass:[ZZCLoginController class]]) {
                        [self.navigationController popToViewController:controller animated:YES];
                        //这里可以做回到登录界面的一些数据操作
                    }
                }
            }
        }
    
    }];

}
@end
